package com.ruoyi.web.controller.note;

import java.util.List;

import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.note.domain.TQuestion;
import com.ruoyi.note.service.ITQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 题目Controller
 *
 * @author cl
 * @date 2021-04-20
 */
@RestController
@RequestMapping("/note/question")
public class TQuestionController extends BaseController {
    @Autowired
    private ITQuestionService tQuestionService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询题目列表
     */
    @PreAuthorize("@ss.hasPermi('note:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(TQuestion tQuestion, HttpServletRequest request) {
        tQuestion.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        startPage();
        List<TQuestion> list = tQuestionService.selectTQuestionList(tQuestion);
        return getDataTable(list);
    }

    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('note:question:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TQuestion tQuestion, HttpServletRequest request) {
        tQuestion.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        List<TQuestion> list = tQuestionService.selectTQuestionList(tQuestion);
        ExcelUtil<TQuestion> util = new ExcelUtil<TQuestion>(TQuestion.class);
        return util.exportExcel(list, "题目数据");
    }

    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tQuestionService.selectTQuestionById(id));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('note:question:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TQuestion tQuestion, HttpServletRequest request) {
        tQuestion.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        return toAjax(tQuestionService.insertTQuestion(tQuestion));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('note:question:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TQuestion tQuestion, HttpServletRequest request) {
        tQuestion.setUserId(tokenService.getLoginUser(request).getUser().getUserId());

        return toAjax(tQuestionService.updateTQuestion(tQuestion));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('note:question:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tQuestionService.deleteTQuestionByIds(ids));
    }
}
